/*
 * Jp_PlanningReporting.java
 *
 * Created on 1 mai 2008, 12:37
 */

package gui;

import IBO.IIntervention;
import IBO.ITechnicien;
import IBO.IZoneGeographique;
import boerrors.AdresseInexistantException;
import boerrors.CodePostalMinimumMultiplicityException;
import boerrors.ZoneMinimumMultiplicityException;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  olivierm
 */
public class Jp_PlanningReporting extends javax.swing.JPanel {

    private Frm_Application _parent = null;
    private Vector<IZoneGeographique> _zones = null;
    private DefaultComboBoxModel _modeleZone = new DefaultComboBoxModel();
    DefaultTableModel _modeleTable = new DefaultTableModel();
    private String[] _colName = {"Matricule","Nom","Prénom","Zone","Numéro Intervention", "Date demande", "Date Intervention", "Priorité intervention","Zone intervention"};
    
    
    public void initComponents2(Frm_Application parent){
            this._parent = parent;
            jXDatePicker_Debut.setDate(new java.util.Date());
            this._modeleTable.setColumnIdentifiers(_colName);

    }    

    public void setZones(){
        try {

            this._modeleZone.removeAllElements();
            this._zones = this._parent.getFactoryRmi().getGestionEmployes().listerZone();
            for (int i = 0; i < this._zones.size(); i++) {
                this._modeleZone.addElement(this._zones.get(i).getLibelleZone());
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Jp_PlanningReporting.class.getName()).log(Level.SEVERE, null, ex);
        }

    }    
    
    /** Creates new form Jp_PlanningReporting */
    public Jp_PlanningReporting() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXTitledPanel1 = new org.jdesktop.swingx.JXTitledPanel();
        jXStatusBar1 = new org.jdesktop.swingx.JXStatusBar();
        jLabel5 = new javax.swing.JLabel();
        jlbl_NbrClient = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jbtn_Fermer = new javax.swing.JButton();
        jXTaskPane1 = new org.jdesktop.swingx.JXTaskPane();
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jcbb_NbrInterventions = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jcbb_Zone = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jXDatePicker_Debut = new org.jdesktop.swingx.JXDatePicker();
        jbtn_Rechercher = new javax.swing.JButton();
        jCheckBox_Zones = new javax.swing.JCheckBox();

        setLayout(new java.awt.BorderLayout());

        jPanel7.setBackground(new java.awt.Color(0, 102, 153));
        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel11.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("Planning général");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addContainerGap(537, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addContainerGap())
        );

        add(jPanel7, java.awt.BorderLayout.NORTH);

        jXPanel1.setLayout(new java.awt.BorderLayout());

        jXTitledPanel1.setTitle("Tableau du planning des interventions");
        jXTitledPanel1.getContentContainer().setLayout(new java.awt.BorderLayout());

        jLabel5.setText("Nombre d'éléments:");
        jXStatusBar1.add(jLabel5);
        jXStatusBar1.add(jlbl_NbrClient);

        jXTitledPanel1.getContentContainer().add(jXStatusBar1, java.awt.BorderLayout.SOUTH);

        jTable1.setModel(_modeleTable);
        jScrollPane1.setViewportView(jTable1);

        jXTitledPanel1.getContentContainer().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jXPanel1.add(jXTitledPanel1, java.awt.BorderLayout.CENTER);

        jbtn_Fermer.setText("Fermer");
        jbtn_Fermer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_FermerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(588, Short.MAX_VALUE)
                .addComponent(jbtn_Fermer)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jbtn_Fermer, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jXPanel1.add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jXTaskPane1.setTitle("Critères de recherche");

        jXPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jcbb_NbrInterventions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Aucune intervention", "1 intervention", "2 interventions", "3 interventions", "4 interventions", "Plus de 4 interventions", "Moins de 4 interventions" }));

        jLabel1.setText("Zone:");

        jLabel2.setText("Nombres d'interventions:");

        jcbb_Zone.setModel(_modeleZone);

        jLabel3.setText("Date de début:");

        jbtn_Rechercher.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/eye.png"))); // NOI18N
        jbtn_Rechercher.setText("Recherche");
        jbtn_Rechercher.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_RechercherActionPerformed(evt);
            }
        });

        jCheckBox_Zones.setText("Toutes les zones");
        jCheckBox_Zones.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox_ZonesItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jXPanel3Layout = new javax.swing.GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jXPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbb_NbrInterventions, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jXPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(51, 51, 51)
                        .addComponent(jXDatePicker_Debut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel3Layout.createSequentialGroup()
                        .addComponent(jcbb_Zone, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox_Zones))
                    .addComponent(jbtn_Rechercher))
                .addContainerGap(141, Short.MAX_VALUE))
        );
        jXPanel3Layout.setVerticalGroup(
            jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbb_Zone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jcbb_NbrInterventions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox_Zones))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jbtn_Rechercher)
                    .addComponent(jXDatePicker_Debut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jXTaskPane1.getContentPane().add(jXPanel3);

        jXPanel1.add(jXTaskPane1, java.awt.BorderLayout.PAGE_START);

        add(jXPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtn_FermerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_FermerActionPerformed
        
        this.setVisible(false);
        
    }//GEN-LAST:event_jbtn_FermerActionPerformed

    private void jbtn_RechercherActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_RechercherActionPerformed
        try {
            this._modeleTable.setRowCount(0);
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String zoneSelectionnee = null;
           if (jCheckBox_Zones.isSelected() == false)
               zoneSelectionnee = this._modeleZone.getSelectedItem().toString();
            Vector<ITechnicien> listeTechniciens = this._parent.getFactoryRmi().getGestionPlanning().getPlanningTechniciens(jcbb_NbrInterventions.getSelectedIndex(), zoneSelectionnee, jXDatePicker_Debut.getDate());
            System.out.println("DEBUG: ListeTechnicien: " + listeTechniciens.size());
            for (int i = 0; i < listeTechniciens.size(); i++){
                if (listeTechniciens.get(i).getListeIInterventions().isEmpty()){
                    String[] objTemp = new String[9];
                    objTemp[0] = String.valueOf(listeTechniciens.get(i).getMatricule());
                    objTemp[1] = listeTechniciens.get(i).getNom();
                    objTemp[2] = listeTechniciens.get(i).getPrenom();
                    objTemp[3] = listeTechniciens.get(i).getIZone().getLibelleZone();
                    objTemp[4] = "";                    
                    objTemp[5] = ""; 
                    objTemp[6] = "";
                    objTemp[7] = "";
                    objTemp[8] = "";
                    this._modeleTable.addRow(objTemp);                    
                }else{
                    Vector<IIntervention> lstIntervention = listeTechniciens.get(i).getListeIInterventions();
                    for (int j = 0; j < lstIntervention.size(); j++){
                        String[] objTemp = new String[9];
                        objTemp[0] = String.valueOf(listeTechniciens.get(i).getMatricule());
                        objTemp[1] = listeTechniciens.get(i).getNom();
                        objTemp[2] = listeTechniciens.get(i).getPrenom();
                        objTemp[3] = listeTechniciens.get(i).getIZone().getLibelleZone();
                        objTemp[4] = String.valueOf(lstIntervention.get(j).getIdIntervention());
                        Date dateDate = lstIntervention.get(j).getDateDemande().getTime();
                        String dateFormatee = dateFormat.format(dateDate);                    
                        objTemp[5] = dateFormatee;
                        Date dateDate2 = lstIntervention.get(j).getDateIntervention().getTime();
                        String dateFormatee2 = dateFormat.format(dateDate2); 
                        objTemp[6] = dateFormatee2;
                        objTemp[7] = lstIntervention.get(j).getPrioriteIntervention();
                        objTemp[8] = lstIntervention.get(j).getIAdresse().getICodePostal().getZone().getLibelleZone();
                        this._modeleTable.addRow(objTemp);                     
                    }                    
                }

               
            }
            jlbl_NbrClient.setText(String.valueOf(this._modeleTable.getRowCount()));
        } catch (CodePostalMinimumMultiplicityException ex) {
            Logger.getLogger(Jp_PlanningReporting.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AdresseInexistantException ex) {
            Logger.getLogger(Jp_PlanningReporting.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ZoneMinimumMultiplicityException ex) {
            Logger.getLogger(Jp_PlanningReporting.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Jp_PlanningReporting.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jbtn_RechercherActionPerformed

    private void jCheckBox_ZonesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox_ZonesItemStateChanged

        if (jCheckBox_Zones.isSelected()){
            jcbb_Zone.setEnabled(false);
        }else{
            jcbb_Zone.setEnabled(true);
        }
        
    }//GEN-LAST:event_jCheckBox_ZonesItemStateChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox_Zones;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker_Debut;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private org.jdesktop.swingx.JXStatusBar jXStatusBar1;
    private org.jdesktop.swingx.JXTaskPane jXTaskPane1;
    private org.jdesktop.swingx.JXTitledPanel jXTitledPanel1;
    private javax.swing.JButton jbtn_Fermer;
    private javax.swing.JButton jbtn_Rechercher;
    private javax.swing.JComboBox jcbb_NbrInterventions;
    private javax.swing.JComboBox jcbb_Zone;
    private javax.swing.JLabel jlbl_NbrClient;
    // End of variables declaration//GEN-END:variables
    
}
